package com.example.demo.Service;

import com.example.demo.Entity.Doctor;
import com.example.demo.Entity.User;
import com.example.demo.Entity.transfer.UserDTO;
import com.example.demo.Mapper.DoctorMapper;
import com.example.demo.Mapper.UserMapper;
import com.example.demo.util.CommonConstants;
import com.example.demo.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    private UserMapper usermapper;


    @Autowired
    private DoctorMapper dockermapper;



    public Map<String, Object> getUserInfoByPhone(String phone){
        Map<String, Object> map = new HashMap<>(5);
        map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
        map.put(CommonConstants.RESP_MESSAGE, "请求成功");
        map.put(CommonConstants.RESULT, usermapper.getUserInfoByPhone(phone));
        return map;
    }



    public Map<String, Object> QueryUserLogin(String phone, String password, int  role) {
        Map<String, Object> map = new HashMap<>(5);
        String pass = Md5Util.getMD5(password);
        if(role == 0){
            User user = usermapper.getUserInfoforLogin(phone, pass);
            if (user != null) {
                map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
                map.put(CommonConstants.RESP_MESSAGE, "登陆成功");
                map.put(CommonConstants.RESULT, "true");
                return map;
            } else {
                map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
                map.put(CommonConstants.RESP_MESSAGE, "账号或密码错误");
                map.put(CommonConstants.RESULT, "false");
                return map;
            }
        }else if(role == 1){
            Doctor docter = dockermapper.getDoctorInfoforLogin(phone, pass);
            if (docter != null) {
                map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
                map.put(CommonConstants.RESP_MESSAGE, "登陆成功");
                map.put(CommonConstants.RESULT, "true");
                return map;
            } else {
                map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
                map.put(CommonConstants.RESP_MESSAGE, "账号或密码错误");
                map.put(CommonConstants.RESULT, "false");
                return map;
            }
        }else{
            map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
            map.put(CommonConstants.RESP_MESSAGE, "请选择您得登陆身份");
            map.put(CommonConstants.RESULT, "false");
            return map;
        }
    }


    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> Userregisters(UserDTO userDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (userDTO.getRole() == 0) {
            User user = usermapper.getUserInfoByPhone(userDTO.getPhone());
            if (user != null) {
                map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
                map.put(CommonConstants.RESP_MESSAGE, "该手机号已经注册，请确认手机号");
                return map;
            } else {
                try {
                    userDTO.setPassWord(Md5Util.getMD5(userDTO.getPassWord()));
                    usermapper.addUser(userDTO);
                    map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
                    map.put(CommonConstants.RESP_MESSAGE, "注册成功");
                    return map;
                } catch (Exception e) {
                    e.printStackTrace();
                    map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
                    map.put(CommonConstants.RESP_MESSAGE, "注册异常，请稍后重试");
                    return map;
                }
            }
        } else if (userDTO.getRole() == 1) {
            Doctor docker = dockermapper.getDoctorInfoByPhone(userDTO.getPhone());
            if (docker != null) {
                map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
                map.put(CommonConstants.RESP_MESSAGE, "该手机号已经注册，请确认手机号");
                return map;
            } else {
                try {
                    userDTO.setPassWord(Md5Util.getMD5(userDTO.getPassWord()));
                    dockermapper.addDoctor(userDTO);
                    map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
                    map.put(CommonConstants.RESP_MESSAGE, "注册成功");
                    return map;
                } catch (Exception e) {
                    e.printStackTrace();
                    map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
                    map.put(CommonConstants.RESP_MESSAGE, "注册异常，请稍后重试");
                    return map;
                }
            }
        } else {
            map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
            map.put(CommonConstants.RESP_MESSAGE, "请选择您的注册身份");
            return map;
        }
    }




}
